CONTENTS | INDEX | PREV | NEXT
 strncpy

   NAME
    strncpy - copy a string returning a pointer to the beginning of the
          destination until nul or the specified number of characters
          is reached.

   SYNOPSIS
    char *ptr = strncpy(d, s, n);
    char *d;
    const char *s;
    int n;

   FUNCTION
    Copy the nul terminated string pointed to by s to the buffer d. The
    nul is normally copied.  The first argument is returned (a pointer
    to the buffer d).

    The copy will also be terminated if the specified maximum is reached,
    in which case the nul is NOT copied.

   EXAMPLE
    #include <stdio.h>
    #include <string.h>
    #include <assert.h>

    /*
     *  This is a dumb example
     */

    main()
    {
        char *buf1 = "hello";
        char *buf2 = "123";
        char dest[32];
        char *ptr;

        strncpy(dest, buf1, 8);
        strcat(dest, buf2);
        puts(dest);                     /* hello123 */

        dest[2] = 23;
        strncpy(dest, buf1, 2);
        assert(dest[2] == 23);          /* it only copied to chars!     */

        dest[2] = 0;            /*  note we have to add the nul */
        strcat(dest, buf2);             /*  he123   */
        puts(dest);

        return(0);
    }

   INPUTS
    char *d;    pointer to beginning of destination buffer
    char *s;    pointer to beginning of source string
    int len;    maximum number of characters to copy

   RESULTS
    char *ptr;  same as the destination buffer pointer (d).

   SEE ALSO
    stpcpy, strcpy